# ======================================================================
#
# Copyright (C) ADN
# ADN::LIBRARY::ADAPTERS::Protocol Protocol.pm
# Description:
#  - Gestion des connexions
#
# ======================================================================

package ADN::LIBRARY::ADAPTERS::Protocol;

use strict;

use ADN::LIBRARY::TELNET::ClientTEL;
use ADN::LIBRARY::SSH::ClientSSH;

# #######################
# CREATE OBJECT
# #######################
sub new {
	# Construction de l'objet $self
	my $self = shift;
	my $class = ref($self) || $self;
	return $self if ref $self;
	$self = bless {} => $class;
	@{$self->{verif_protocol}} = ('ssh', 'telnet');
	$self->{stop_protcole} = 0;
	$self->{ligne_cred} = 0;
	return $self;
}

# #######################
# MAIN
# #######################
sub start
{
    my $self = shift;
    my ($ligne, $ligne2);
    ( $self->{host}, $self->{credential}, $self->{protocol}, $self->{connexion} ) = @_;

    if (defined $self->{protocol} && ($self->{protocol} ne '')) {
			@{$self->{tab_protocol}} = split /,/smx, $self->{protocol};
			foreach $ligne (@{$self->{tab_protocol}}) {
				$ligne =~ tr/[A-Z]/[a-z]/;
				foreach $ligne2 (@{$self->{verif_protocol}}) {
					if ($ligne =~ m/^$ligne2$/smx) {
        		$self->$ligne(); 
						return ($self->{connexion}, \%{$self->{hash_credential}}, $ligne) if (defined($self->{connexion}) && ($self->{connexion} !~ m/^$/) );						
					}
				}
			}
		}
  	return (undef, undef, undef);
}

# #######################
# CONNECT SSH
# #######################
sub ssh
{
	my $self = shift;

	if (defined $self->{connexion} && ($self->{connexion} ne '')) {
    $self->{connexion}->disconnect();
    undef($self->{connexion});
	}
	$self->credential(0);
	if ($self->{stop_protocole} == 0) {
    $self->{connexion} = ADN::LIBRARY::SSH::ClientSSH->new($self->{host}, 
																													 $self->{hash_credential}{username}, 
																													 $self->{hash_credential}{password});
    if (!defined $self->{connexion}) {
      $self->ssh();
		}
	}
	$self->{stop_protocole} = 0;
	return;
}

# #######################
# CONNECT TELNET
# #######################
sub telnet
{
	my $self = shift;

	if (defined $self->{connexion} && ($self->{connexion} ne '')) {
    $self->{connexion}->disconnect();
    undef($self->{connexion});
	}
	if ($self->{stop_protocole} == 0) {
		$self->{connexion} = ADN::LIBRARY::TELNET::ClientTEL->new($self->{host});
		if (defined $self->{connexion} && ($self->{connexion} ne '')) {
			$self->credential(1);
		}
	}
	return;
}

# #######################
# CREDENTIAL
# #######################
sub credential
{
  my $self = shift;
  my (@tab, @tab2);

  if ($self->{credential} !~ /^$/smx) {
    @tab = split /\#ADN\#/smx, $self->{credential};
    if (defined $tab[$self->{ligne_cred}]) {
      @tab2 = split /:ADN:/smx, $tab[$self->{ligne_cred}];
      $self->{hash_credential}{username} = $tab2[0];
      $self->{hash_credential}{password} = $tab2[1];
      $self->{hash_credential}{enable} = $tab2[2];
      $self->{ligne_cred}++;
      if (!defined $tab[$self->{ligne_cred}] && $_[0]) {
        $self->{stop_protocole} = 1;
      	$self->{ligne_cred} = 0;
      }
    }
    else
    {
      @tab = @tab2 = undef;
      $self->{stop_protocole} = 1;
      $self->{ligne_cred} = 0;
    }
  }
  @tab = @tab2 = undef;
  return;
}

# #######################
# DESTROY OBJECT
# #######################
sub DESTROY {
	my $self = shift;
	return;
}
1;
__END__
